<template>
  <div>
    <div>
    <el-form :model="form" inline >
    <el-form-item >
      <el-input v-model="form.name" placeholder='输入分组名称'/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">保存</el-button>
    </el-form-item>
  </el-form>
  </div>
  <el-table
    ref="multipleTableRef"
    :data="dataTable"
    border
    style="width:500px" max-height="650"
  >

    <el-table-column label="分组名称" width="120">
      <template #default="scope">{{ scope.row.name }}</template>
    </el-table-column>
    <el-table-column  label="操作" >
      <template #default="scope">
        <el-button
          link
          type="primary"
          size="small"
          @click.prevent="fenpei(scope.row)"
        >
          分配权限
        </el-button>
        <el-button
          link
          type="primary"
          size="small"
          @click.prevent="openSet(scope.row)"
        >
          编辑
        </el-button>
        <el-button
          link
          type="primary"
          size="small"
          @click.prevent="deleteRow(scope.row)"
        >
          删除
        </el-button>
      </template>
    </el-table-column>

  </el-table>

  <el-dialog
    v-model="dialogVisibleFZ"
    width="500"
  >
    <span>
      <el-form-item >
      <el-input v-model="dataTableForm.name" placeholder='输入分组名称'/>
    </el-form-item>
    </span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisibleFZ = false">取消</el-button>
        <el-button type="primary" @click="submintSet">
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>
  </div>
</template>

<script setup >
import { ref, onBeforeMount } from 'vue'
import { sysUserGrouplist, sysUserGroupadd, sysUserGroupdelete, sysUserGroupupdate } from '@/api/index'
import { ElMessage } from 'element-plus'
const dataTable = ref([])
const form = ref({})
onBeforeMount(() => {
  getList()
})
const getList = () => {
  sysUserGrouplist().then(res => {
    const { data: { data } } = res
    dataTable.value = data
  })
}

const onSubmit = () => {
  sysUserGroupadd(form.value).then(res => {
    ElMessage.success('操作成功')
    getList()
  })
}
const fenpei = () => {
  ElMessage.success('分配全部权限成功')
}
const deleteRow = (row) => {
  sysUserGroupdelete({ id: row.id }).then(() => {
    ElMessage.success('操作成功')
    getList()
  })
}
const dialogVisibleFZ = ref(false)
const dataTableForm = ref({ name: '', id: '' })
const openSet = (row) => {
  // sysUserGroupupdate
  dataTableForm.value.name = row.name
  dataTableForm.value.id = row.id
  dialogVisibleFZ.value = true
}
const submintSet = () => {
  sysUserGroupupdate(dataTableForm.value).then(res => {
    dialogVisibleFZ.value = false
    getList()
    ElMessage.success('操作成功')
  })
}
</script>

<style lang='less' scoped>

</style>
